package com.zbest.zpay.paycore.dal.dao;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;

import com.zbest.common.lang.enums.BizCode;
import com.zbest.common.lang.exception.ZpayException;
import com.zbest.zpay.paycore.dal.mapper.FluxInfoMapper;
import com.zbest.zpay.paycore.dal.model.FluxInfoDO;

/**
 * Created by wenfeihu on 16/5/20.
 */
@Service("fluxInfoDAO")
public class FluxInfoDAO {

    @Resource
    private FluxInfoMapper fluxInfoMapper;

    public void insert(FluxInfoDO fluxInfoDO) {

        try {
            fluxInfoMapper.insert(fluxInfoDO);
        } catch (DataIntegrityViolationException e) {
            throw new ZpayException(BizCode.DUPLICATED_ERROR);
        }
    }

    public void update(String fluxId, String destStatus, String srcStatus) {
        int row = fluxInfoMapper.updateStatus(fluxId, destStatus, srcStatus, new Date());
        if (row <= 0) {
            throw new ZpayException(BizCode.DATA_NOT_EXISTS);
        }

    }

    public void updateResponseAndStatus(String fluxId, String respCode, String respMessage,
                                        String instRespCode, String instRespMessage,
                                        String destStatus, String srcStatus) {

        int row = fluxInfoMapper.updateResponseAndStatus(fluxId, respCode, respMessage,
            instRespCode, instRespMessage, destStatus, srcStatus, new Date());
        if (row <= 0) {
            throw new ZpayException(BizCode.DATA_NOT_EXISTS);
        }
    }

    public FluxInfoDO query(String fluxId) {

        return fluxInfoMapper.query(fluxId);
    }

    public List<FluxInfoDO> queryByChannelStatus(String channelId, String status, Date beginDate,
                                                 Date endDate) {

        return fluxInfoMapper.queryByChannelStatus(channelId, status, beginDate, endDate);
    }

    public FluxInfoDO queryBySerialNo(String instSerialNo, String channelId) {

        return fluxInfoMapper.queryBySerialNo(instSerialNo, channelId);
    }
}
